﻿using System;
using System.IO;
using System.Text;
using System.Collections.Generic;
using libsharp;

namespace libwutong
{
    class MyTxt
    {
        static public string FolderConDition { get { return "处理条件/"; } }
        static public string IdField { get { return "运单编号"; } }
        static public string SignField { get { return "签收网点"; } }
        static public string ReceiveStaffField { get { return "派件或收件员"; } }
        static public string WeightField { get { return "重量"; } }
        static public string ScantTypeField { get { return "扫描类型"; } }
        static public string ScanNodeField { get { return "扫描网点"; } }
        static public string ScanTimeField { get { return "扫描时间"; } }
        static public string ScanStaffField { get { return "扫描雇员"; } }

        public delegate bool CallBack(string line, int iTarget);


        static public void print()
        {
            Console.WriteLine(" 检查筛选字段:");
            Console.Write(" " + IdField);
            Console.Write(" " + SignField);
            Console.Write(" " + ReceiveStaffField);
            Console.Write(" " + WeightField);
            Console.Write(" " + ScantTypeField);
            Console.Write(" " + ScanNodeField);
            Console.Write(" " + ScanTimeField);
            Console.Write(" " + ScanStaffField);
            Console.WriteLine("");
        }

        static public void printf(string filename, int i)
        {
            Console.WriteLine(" 解析文本失败({0} 第{1}行)", filename, i);
        }

        static public string deleteAreaWord(string str)
        {
            str = str.Replace("省", "");
            str = str.Replace("市", "");
            str = str.Replace("辖", "");
            str = str.Replace("区", "");
            str = str.Replace("自治", "");
            str = str.Replace("县", "");
            str = str.Replace("镇", "");
            str = str.Replace("村", "");
            return str;
        }

        static public string[] split(string line, params char[] separator)
        {
            string[] list0 = line.Split(separator);
            List<string> list = new List<string>();
            string str;
            for (int i = 0; i < list0.Length; ++i)
            {
                str = list0[i].Trim();
                if (str.Length > 0)
                {
                    list.Add(str);
                }
            }
            if (list.Count < 1)
            {
                return null;
            }
            return list.ToArray();
        }


        static public bool read(string filename, CallBack cb, int iTarget = 0)
        {
            StreamReader sr = new StreamReader(filename, Encoding.Default);
            if (sr == null)
            {
                return false;
            }
            int i = 0;
            int itarget = 0;
            string line = sr.ReadLine();
            while (line != null)
            {
                line = line.Trim();
                ++i;
                if (line.Length == 0)
                {
                    line = sr.ReadLine().Trim();
                    continue;
                }
                if (!cb(line, itarget))
                {
                    printf(filename, i);
                    sr.Close();
                    return false;
                }
                if (iTarget > 0 && ++itarget >= iTarget)
                {
                    sr.Close();
                    return true;
                }
                line = sr.ReadLine();
            }
            sr.Close();
            if (iTarget > 0 && itarget < iTarget)
            {
                printf(filename, i);
                return false;
            }
            return true;
        }
    }


    class TxtWeight
    {
        string _scanTypeArrive;
        string _scanTypeReceive;
        string _scanNodeArrive;
        string _scanNodeReceive;

        public string ScantTypeArrive { get { return _scanTypeArrive; } }
        public string ScantTypeReceive { get { return _scanTypeReceive; } }
        public string ScanNodeArrive { get { return _scanNodeArrive; } }
        public string ScanNodeReceive { get { return _scanNodeReceive; } }

        public void print()
        {
            Console.WriteLine(" 检查重量筛选:");
            Console.WriteLine(" {0}: {1}",_scanTypeReceive, _scanNodeReceive);
            Console.WriteLine(" {0}: {1}", _scanTypeArrive, _scanNodeArrive);
        }

        public TxtWeight()
        {
            MyTxt.CallBack cb = (string line, int iTarget) =>
            {
                string[] list = MyTxt.split(line, ':', '：');
                if (list == null || list.Length < 2)
                {
                    return false;
                }
                if (iTarget == 0)
                {
                    _scanTypeReceive = list[0];
                    _scanNodeReceive = list[1];
                }
                else if (iTarget == 1)
                {
                    _scanTypeArrive = list[0];
                    _scanNodeArrive = list[1];
                }
                return true;
            };

            if(!MyTxt.read(MyTxt.FolderConDition + "重量筛选.txt", cb, 2))
            {
                ConsoleSharp.exitByAnykey();
            }
        }
    }



    class TxtPrice
    {
        int _threshold;
        int _billingType;
        int _scanCost;

        public int Threshold { get { return _threshold; } }
        public int BillingType { get { return _billingType; } }
        public int ScanCost { get { return _scanCost; } }

        public void print()
        {
            Console.WriteLine(" 检查价格说明:");
            Console.WriteLine(" 阈值: " + _threshold);
            Console.WriteLine(" 计费方式: " + _billingType);
            Console.WriteLine(" 扫描费用: " + _scanCost);
        }

        public TxtPrice()
        {
            MyTxt.CallBack cb = (string line, int iTarget) =>
            {
                string[] list = MyTxt.split(line, ':', '：');
                if (list == null || list.Length < 2)
                {
                    return false;
                }
                if (iTarget == 0)
                {
                    _threshold = Convert.ToInt32(list[1]);
                }
                else if (iTarget == 1)
                {
                    _billingType = Convert.ToInt32(list[1]);
                }
                else if(iTarget == 2)
                {
                    _scanCost = Convert.ToInt32(list[1]);
                }
                return true;
            };

            if (!MyTxt.read(MyTxt.FolderConDition + "价格表说明.txt", cb, 3))
            {
                ConsoleSharp.exitByAnykey();
            }
        }
    }


    class TxtAreaSupple
    {
        List<string[]> _provinces = new List<string[]>();
        public string Province(string area)
        {
            for (int k = 0; k < _provinces.Count; ++k)
            {
                for (int i = 1; i < _provinces[k].Length; ++i)
                {
                    if (area.IndexOf(_provinces[k][i]) >= 0)
                    {
                        return _provinces[k][0];
                    }
                }
            }
            return null;
        }


        public void print()
        {
            Console.WriteLine(" 检查目的地补充:");
            for (int k = 0; k < _provinces.Count; ++k)
            {
                Console.Write(" {0}: ", _provinces[k][0]);
                for (int i = 1; i < _provinces[k].Length; ++i)
                {
                    Console.Write("{0},", _provinces[k][i]);
                }
                Console.WriteLine("");
            }
        }

        public TxtAreaSupple()
        {
            MyTxt.CallBack cb = (string line, int iTarget) =>
            {
                string[] list = MyTxt.split(line, ':', '：');
                if (list == null || list.Length < 2)
                {
                    return false;
                }
                string province = MyTxt.deleteAreaWord(list[0]);
                list = MyTxt.split(list[1], ',', '，');
                // string[] www = "wwwwww".Split('/');  // 得到自己
                if (list == null)
                {
                    return false;
                }
                List<string> list2 = new List<string>();
                list2.Add(province);
                for (int i = 0; i < list.Length; ++i)
                {
                    list2.Add(MyTxt.deleteAreaWord(list[i]));
                }
                _provinces.Add(list2.ToArray());
                return true;
            };
            if(!MyTxt.read(MyTxt.FolderConDition + "目的地补充.txt", cb))
            {
                ConsoleSharp.exitByAnykey();
            }
        }
    }
}
